package gui;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import logic.Point;

/**
 * template class for buttons
 * @author Rekin
 *
 */
public class CanvasButtonTemplate{
	protected Point topLeft;
	protected int myWidth;
	protected int myHeight;
	protected int myBaselineOffset;
	protected Font myUsedFont;
	protected boolean enabled;
	
	/**
	 * constructs button
	 * @param wid button width
	 * @param hei button height
	 * @param tl top left Point of the button
	 */
	public CanvasButtonTemplate(int wid, int hei, Point tl) {
		topLeft=tl;
		myWidth=wid;
		myHeight=hei;
		myBaselineOffset=hei-1;
		enabled=true;
		countFontPosition();
	}
	
	/**
	 * sets button desired size
	 * @param w button width
	 * @param h button height
	 */
	public void setItemSize(int w, int h){
		myWidth=w;
		myHeight=h;
	}
	
	/**
	 * prepares font sizes to fit the button height
	 */
	private void countFontPosition(){
		myUsedFont=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
		if(myUsedFont.getHeight()>myHeight){ //make font smaller
			myUsedFont=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
		}
		//count baseline position
		int topOffset= (myHeight-myUsedFont.getHeight())/2;
		myBaselineOffset=topOffset+myUsedFont.getBaselinePosition();
	}
	
	/**
	 * paints button
	 * @param g graphic context
	 */
	protected void paintMe(Graphics g){
		g.setColor(255, 0, 0);
		for(int i=0;i<2;++i){
			g.drawRect(i+topLeft.getX(), i+topLeft.getY(), myWidth-i*2, myHeight-i*2);
		}
		g.setColor(250, 200, 20);
		for(int i=0;i<4;++i){
			g.drawLine(2+i+topLeft.getX(), 2+topLeft.getY(), 2+i+topLeft.getX(), myHeight-2+topLeft.getY());
			g.drawLine(myWidth-2-i+topLeft.getX(), 2+topLeft.getY(), myWidth-2-i+topLeft.getX(), myHeight-2+topLeft.getY());
		}
		g.setColor(255, 0, 0);
	}
	
	/**
	 * @return top left corner of the button
	 */
	public Point getTopLeft() {
		return topLeft;
	}
	
	/**
	 * sets top left corner of the button
	 * @param p top left Point
	 */
	public void setTopLeft(Point p){
		topLeft=p;
	}

	/**
	 * @return button width
	 */
	public int getMyWidth() {
		return myWidth;
	}

	/**
	 * @return button height
	 */
	public int getMyHeight() {
		return myHeight;
	}

	/**
	 * @return is enabled
	 */
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * @param enabled
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
}
